import {AdapterRequest, AdapterResponse, RequestConfig,HttpServicesAdapter2} from "../../types";
let mBaseAdapterRequest:AdapterRequest|undefined
export class HttpServicesClient2 implements HttpServicesAdapter2{
	private readonly baseUrl: string | undefined

	constructor(baseUrl?: string) {
		this.baseUrl = baseUrl
	}
	_getRequestRequestConfig(url: string,config?: RequestConfig): RequestConfig {
		const requestUrl = this.baseUrl ? `${this.baseUrl}${url}` : url
		return  {
			url:requestUrl,
			...config,
		} as RequestConfig
	}
	_getRequestRequestConfigWithData(url: string, data?: any, config?: RequestConfig): RequestConfig {
		let requestConfig = this._getRequestRequestConfig(url,config);
		if(data){
			if(typeof data === 'object'){
				requestConfig.data = {
					...requestConfig.data,
					...data};
			}else{
				requestConfig.data = data;
			}
		}
		return requestConfig;
	}
	request<T = any, R = AdapterResponse<T>> (config: RequestConfig): Promise<R>{
		if(mBaseAdapterRequest && typeof mBaseAdapterRequest === 'function'){
			return mBaseAdapterRequest<T,R>(config)
		} else {
			return Promise.reject('HttpClient is undefined')
		}
	}
	get<T = any, R = AdapterResponse<T>>(url: string,params?:Record<string,string> ,config?: RequestConfig): Promise<R>{
		const requestConfig = this._getRequestRequestConfig(url,config)
		requestConfig.params = {
			...requestConfig.params,
			...params};
		requestConfig.method='get'
		return this.request<T, R>(requestConfig)
	}
	delete<T = any, R = AdapterResponse<T>>(url: string, config?: RequestConfig): Promise<R>{
		const requestConfig = this._getRequestRequestConfig(url,config)
		requestConfig.method='delete'
		return this.request<T, R>(requestConfig)
	}
	head<T = any, R = AdapterResponse<T>>(url: string, config?: RequestConfig): Promise<R>{
		const requestConfig = this._getRequestRequestConfig(url,config)
		requestConfig.method='head'
		return this.request<T, R>(requestConfig)
	}
	options<T = any, R = AdapterResponse<T>>(url: string, config?: RequestConfig): Promise<R>{
		const requestConfig = this._getRequestRequestConfig(url,config)
		requestConfig.method='options'
		return this.request<T, R>(requestConfig)
	}
	post<T = any, R = AdapterResponse<T>>(url: string, data?: any, config?: RequestConfig): Promise<R>{
		const requestConfig = this._getRequestRequestConfigWithData(url,data,config)
		requestConfig.method='post'
		return this.request<T, R>(requestConfig)
	}
	put<T = any, R = AdapterResponse<T>>(url: string, data?: any, config?: RequestConfig): Promise<R>{
		const requestConfig = this._getRequestRequestConfigWithData(url,data,config)
		requestConfig.method='put'
		return this.request<T, R>(requestConfig)
	}
	patch<T = any, R = AdapterResponse<T>>(url: string, data?: any, config?: RequestConfig): Promise<R>{
		const requestConfig = this._getRequestRequestConfigWithData(url,data,config)
		requestConfig.method = 'patch'
		return this.request<T, R>(requestConfig)
	}
	/**
	 * 添加请求处理函数(适配平台请求)
	 * @param adapterRequest
	 */
	static addAdapterRequest(adapterRequest:AdapterRequest){
		mBaseAdapterRequest=adapterRequest;
	}
	/**
	 * 基于端口地址创建适配器
	 * @param baseUrl
	 */
	static createHttpServicesAdapter(baseUrl?: string): HttpServicesClient2 {
		return new HttpServicesClient2(baseUrl)
	}
}
